package com.platform.api;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.ApiBaseAction;
import com.platform.utils.IdUtil;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.push.UPush;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 作者: @author zhurusen <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: 圈子管理 <br>
 */
@RestController
@RequestMapping("/api/circle")
public class ApiCircleController extends ApiBaseAction {

    @Autowired
    private ApiUserService apiUserService;
    @Autowired
    private ApiCircleService apiCircleService;
    @Autowired
    private ApiCircleResService apiCircleResService;
    @Autowired
    private ApiCircleBillService apiCircleBillService;
    @Autowired
    private ApiAmountBillService apiAmountBillService;


    /**
     * 发布
     *
     * @param loginUser
     * @param content
     * @param place
     * @param type      类型 1 图片 2 视频 3 文字
     * @param redIds
     * @param duration
     * @return
     */
    @RequestMapping("add")
    public R add(@LoginUser UserVo loginUser, String content, String place, Integer type, String redIds, String duration) {
        ApiCircleVo circleVo = new ApiCircleVo();
        circleVo.setCreateTime(new Date());
        circleVo.setContent(content);
        circleVo.setPlace(place);
        circleVo.setDuration(duration);
        circleVo.setType(type);
        circleVo.setUserId(loginUser.getId());
        apiCircleService.save(circleVo);

        apiCircleResService.saveRes(circleVo, redIds);
        return R.ok(0, "发布成功", "");
    }

    /**
     * 列表
     *
     * @param loginUser
     * @param type      1 精选 2 关注
     * @param page
     * @return
     */
    @RequestMapping("list")
    public R add(@LoginUser UserVo loginUser, Integer type, @RequestParam(value = "page", defaultValue = "1") Integer page) {

        Map params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);

        if (type != null && type == 2 && loginUser != null) {
            params.put("cmUserId", loginUser.getId());
        }

        params.put("sidx", "create_time");
        params.put("order", "desc");
        Query query = new Query(params);

        List<ApiCircleVo> list = apiCircleService.selectList(query);
        for (ApiCircleVo info : list) {
            //发布人的信息
            UserInfo userInfo = apiUserService.toUserInfo(info.getUserId(), loginUser);
            if (userInfo != null) {
                UserInfo vo = new UserInfo();
                vo.setId(userInfo.getId());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickName(userInfo.getNickName());
                vo.setGender(userInfo.getGender());
                vo.setAge(userInfo.getAge());
                vo.setMobile(userInfo.getMobile());
                vo.setLive(userInfo.getLive());
                vo.setLivePrice(userInfo.getLivePrice());
                vo.setWake(userInfo.getWake());
                vo.setWakePrice(userInfo.getWakePrice());
                vo.setGuide(userInfo.getGuide());
                vo.setGuidePrice(userInfo.getGuidePrice());
                vo.setEmotion(userInfo.getEmotion());
                vo.setEmotionPrice(userInfo.getEmotionPrice());
                info.setUserVo(vo);

                Map paramsRes = new HashMap();
                paramsRes.put("circleId", info.getId());
                info.setResVoList(apiCircleResService.selectList(paramsRes));

                //点赞量
                params = new HashMap();
                params.put("circleId", info.getId());
                params.put("type", 3);
                Integer likeNum = apiCircleBillService.selectCount(params);
                info.setLikeNum(likeNum);

                if (loginUser != null) {
                    params = new HashMap();
                    params.put("circleId", info.getId());
                    params.put("userId", loginUser.getId());
                    params.put("type", 3);
                    Integer isLike = apiCircleBillService.selectCount(params);
                    info.setIsLike(isLike > 0 ? 1 : 2);
                } else {
                    info.setIsLike(2);
                }
            }
        }
        return R.ok(0, "获取成功", list);
    }

    /**
     * 指定主播的动态
     *
     * @param loginUser
     * @param userId
     * @param page
     * @return
     */
    @RequestMapping("listByUserId")
    public R listByUserId(@LoginUser UserVo loginUser, Long userId, @RequestParam(value = "page", defaultValue = "1") Integer page) {

        Map params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);

        params.put("sidx", "create_time");
        params.put("order", "desc");

        params.put("userId", userId);

        Query query = new Query(params);

        List<ApiCircleVo> list = apiCircleService.selectList(query);
        for (ApiCircleVo info : list) {
            if (loginUser != null) {
                params = new HashMap();
                params.put("circleId", info.getId());
                params.put("userId", loginUser.getId());
                params.put("type", 3);
                Integer isLike = apiCircleBillService.selectCount(params);
                info.setIsLike(isLike > 0 ? 1 : 2);
            } else {
                info.setIsLike(2);
            }

            //点赞量
            params = new HashMap();
            params.put("circleId", info.getId());
            params.put("type", 3);
            Integer likeNum = apiCircleBillService.selectCount(params);
            info.setLikeNum(likeNum);

            //发布人的信息
            info.setUserVo(apiUserService.toUserInfo(info.getUserId(), loginUser));
            Map paramsRes = new HashMap();
            paramsRes.put("circleId", info.getId());
            info.setResVoList(apiCircleResService.selectList(paramsRes));
        }
        return R.ok(0, "获取成功", list);
    }

    /**
     * 详情页
     */
    @RequestMapping("info")
    public R info(Long id, @RequestParam(value = "type", defaultValue = "1") Integer type, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);

        params.put("sidx", "create_time");
        params.put("order", "desc");
        params.put("type", type);
        params.put("circleId", id);
        Query query = new Query(params);

        List<ApiCircleBillVo> billVos = apiCircleBillService.selectList(query);


        Map<String, Object> objectMap = new HashMap<>();

        for (ApiCircleBillVo info : billVos) {
            //发布人的信息
            UserVo userVo = apiUserService.queryObject(info.getUserId());
            UserInfo userMo = new UserInfo();
            userMo.setAvatar(userVo.getAvatar());
            userMo.setNickName(userVo.getNickname());
            userMo.setGender(userVo.getGender());
            userMo.setAge(userVo.getAge());
            info.setUserVo(userMo);
        }
        objectMap.put("list", billVos);

        Map<String, Object> param_ds = new HashMap();
        param_ds.put("type", 1);
        param_ds.put("circleId", id);
        objectMap.put("dsCount", apiCircleBillService.selectCount(param_ds));

        Map<String, Object> param_pl = new HashMap();
        param_pl.put("type", 2);
        param_pl.put("circleId", id);
        objectMap.put("plCount", apiCircleBillService.selectCount(param_pl));

        Map<String, Object> param_dz = new HashMap();
        param_dz.put("type", 3);
        param_dz.put("circleId", id);
        objectMap.put("dzCount", apiCircleBillService.selectCount(param_dz));


        return R.ok(0, "获取成功", objectMap);
    }


    /**
     * 根据圈子
     *
     * @param loginUser
     * @param type      1打赏 2 评论 3 点赞
     * @param amount
     * @param place
     * @param content
     * @param circleId
     * @return
     */
    @RequestMapping("addBill")
    public R addBill(@LoginUser UserVo loginUser, Integer type,
                     @RequestParam(value = "amount", defaultValue = "0") BigDecimal amount,
                     @RequestParam(value = "place", defaultValue = "") String place,
                     @RequestParam(value = "content", defaultValue = "") String content, Long circleId) {
        try {

            Boolean addBoolean = true;
            ApiCircleVo apiCircleVo = apiCircleService.selectOne(circleId);
            Map<String, Object> params = new HashMap();
            params.put("type", type);
            params.put("circleId", circleId);


            if (type != null && type == 1) {
                apiAmountBillService.consume(apiCircleVo.getUserId(), "circleReward", amount, loginUser);
                /**
                 * 推送消息
                 */

            }

            if (type != null && type == 3) {
                params.put("userId", loginUser.getId());
                ApiCircleBillVo apiCircleBillVo = apiCircleBillService.selectByTypeAndId(params);
                if (apiCircleBillVo != null) {
                    addBoolean = false;
                }

            }

            if (addBoolean) {
                ApiCircleBillVo billVo = new ApiCircleBillVo();
                billVo.setCreateTime(new Date());
                billVo.setType(type);
                billVo.setAmount(amount);
                billVo.setCircleId(circleId);
                billVo.setContent(content);
                billVo.setPlace(place);
                billVo.setUserId(loginUser.getId());
                apiCircleBillService.save(billVo);
            }

            /**
             * 返回当前会员的余额
             */
            Map<String, Object> param = new HashMap();
            param.put("type", type);
            param.put("circleId", circleId);
            Integer count = apiCircleBillService.selectCount(param);
            UserVo userVo = apiUserService.queryObject(loginUser.getId());
            Map<String, Object> reObject = new HashMap<>();
            reObject.put("count", count + "");
            reObject.put("availableAmount", userVo.getAvailable_amount().stripTrailingZeros().toPlainString());

            return R.ok(0, "获取成功", reObject);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ok(12, "系统偷懒了", "");
        }


    }

    /**
     * 取消点赞
     *
     * @param loginUser
     * @param id
     * @return
     */
    @RequestMapping("/cancelLike")
    public R cancelFollow(@LoginUser UserVo loginUser, Long id) {
        Map<String, Object> params = new HashMap();
        params.put("type", 3);
        params.put("userId", loginUser.getId());
        params.put("circleId", id);

        ApiCircleBillVo apiCircleBillVo = apiCircleBillService.selectByTypeAndId(params);
        if (apiCircleBillVo != null) {
            apiCircleBillService.del(apiCircleBillVo.getId());
        }

        Map<String, Object> param = new HashMap();
        param.put("type", 3);
        param.put("circleId", id);

        Integer count = apiCircleBillService.selectCount(param);

        return R.ok(0, "成功取消", count);
    }
}